//
//  ViewController.m
//  Twitter
//
//  Created by iD Student on 7/9/13.
//  Copyright (c) 2013 jasonkurtzman. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(canTweetStatus) name:ACAccountStoreDidChangeNotification object:nil];
    
	// Do any additional setup after loading the view, typically from a nib.
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    [self canTweetStatus];
}

-(void)canTweetStatus
{
    if([TWTweetComposeViewController canSendTweet])
    {
        self.easyTweetButton.enabled = YES;
        self.easyTweetButton.alpha = 1.0f;
        self.customTweetButton.enabled = YES;
        self.customTweetButton.alpha = 1.0f;
    }else
    {
        self.easyTweetButton.enabled = NO;
        self.easyTweetButton.alpha = 0.5f;
        self.customTweetButton.enabled = NO;
        self.customTweetButton.alpha = 0.5f;
    }
}

-(IBAction)sendEasyTweet:(id)sender
{
    //Set up the built-in twitter composition view controller
    TWTweetComposeViewController *tweetViewController = [[TWTweetComposeViewController alloc] init];
    
    //Set the initial tweet text
    [tweetViewController setInitialText:_outputTextView.text];
    
    //Create the completion handler block
    [tweetViewController setCompletionHandler:^(TWTweetComposeViewControllerResult result)
    {
        NSString *output;
        
        switch (result) {
            case TWTweetComposeViewControllerResultCancelled:
                //Cancel Button Tapped
                output = @"Tweet Canceled";
                break;
            case TWTweetComposeViewControllerResultDone:
                output = @"Tweet Done";
                break;
            default:
                break;
        }
        
        [self performSelectorOnMainThread:@selector(displayText:) withObject:output waitUntilDone:NO];
        [self dismissModalViewControllerAnimated:YES];
        
    }];
    
    [self presentModalViewController:tweetViewController animated:YES];
}

-(BOOL) textFieldShouldReturn:(UITextField *)textField{
    
    [_outputTextView resignFirstResponder];
    return YES;
}

-(void) displayText:(NSString *)text
{
    self.outputTextView.text = text;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
